<?php ;

class BasePage {
    public $smarty;
    public $theme;

    public $center = '$center';
    public $name = '$name';
    public $needAdmin = false;
    public $title = '$title';

    public $deniedCenter = 'admin_templates/admin_access_denied';

    public $errors = array();

    public function __construct($smarty, $theme) {
        $this->smarty = $smarty;
        $this->theme = $theme;
    }

    /** do page actions */
    public function process() {}

    /** show full page */
    public function render() {
        $smarty = $this->smarty;

        // ugh
        if (! defined('pagename')) {
            define('pagename', $this->name);
        }

        $smarty->assign('pagename', $this->name);

        $title = $this->lget($this->title);
        $smarty->assign('posttitle', ' / ' . $title);

        do_sidebar($smarty);

        $center = $this->center;
        if ($this->needAdmin && ! $this->forceAdmin()) {
            $center = $this->deniedCenter;
        }

        $alert = join('<br/>', array_keys($this->errors));
        $smarty->assign('alert', $alert);

        $smarty->assign('tpl_center', $this->tfile($center));
        $smarty->display($this->tfile('pligg.tpl'));
    }

    public function error($message) {
        $this->errors[htmlspecialchars($message)] = 1;
    }

    //----------------

    /** return translation of $message */
    protected function lget($message) {
        $val = $this->smarty->get_config_vars($message);
        return $val ? $val : $message;
    }

    /** return path of $template in the current theme */
    protected function tfile($template) {
        return mnmpath . "templates/{$this->theme}/$template";
    }

    /** return true if user is admin or god.  doesn't force login. */
    protected function isAdmin() {
        $value = checklevel('god') || checklevel('admin');
        $this->smarty->assign('isAdmin', (int) $value);
        return $value;
    }

    /** return true if user is admin or god.  forces login. */
    protected function forceAdmin() {
        force_authentication();
        return $this->isAdmin();
    }

    /** return base url of this page */
    protected function selfUrl() {
        $url = $_SERVER['REQUEST_URI'];
        $url = preg_replace('{[?].*}', '', $url);
        return $url;
    }
}
